import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.*;
import java.net.URL;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import javax.imageio.ImageIO;
import javax.swing.text.AttributeSet;
import javax.swing.text.html.HTMLDocument;

public class MainClass {
	static String folder;
   public static void main(String[] args) 
   throws Exception {
	   
	   int count=0;
	  Scanner input = new Scanner(System.in);
	  String page="";  
	  String zero="0";
	  while(page.compareTo(zero)!=0){
		  System.out.println("Enter a web page url: ");
		  page = input.nextLine();
	  if(page.compareTo(zero)==0)
	  {break;}
	  System.out.println("Enter the name of the new folder:");
	  folder = input.nextLine();
	  File dir = new File(""+folder);
	  dir.mkdir();
      URL url = new URL(page);
      BufferedReader reader = new BufferedReader
      (new InputStreamReader(url.openStream()));
      BufferedWriter writer = new BufferedWriter
      (new FileWriter(String.format("%s\\%d.html", folder, count)));
      String line;
      while ((line = reader.readLine()) != null) {
        // System.out.println(line);
         writer.write(line);
         writer.newLine();
      
	  }
      reader.close();
      writer.close();
      count++;
	  
	  //2nd part with images
	// String webUrl = "http://www.bb-team.org/workout/4636_sila-i-hipertrofiya-za-edin-mesec";
      URL urls = new URL(page);
      URLConnection connection = urls.openConnection();
      InputStream is = connection.getInputStream();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);

      HTMLEditorKit htmlKit = new HTMLEditorKit();
      HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
      HTMLEditorKit.Parser parser = new ParserDelegator();
      HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
      parser.parse(br, callback, true);

      for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next()) {
          AttributeSet attributes = iterator.getAttributes();
          String imgSrc = (String) attributes.getAttribute(HTML.Attribute.SRC);

          if (imgSrc != null && (imgSrc.endsWith(".jpg") || (imgSrc.endsWith(".png")) || (imgSrc.endsWith(".jpeg")) || (imgSrc.endsWith(".bmp")) || (imgSrc.endsWith(".ico")))) {
              try {
                  downloadImage(page, imgSrc);
              } catch (IOException ex) {
                  System.out.println(ex.getMessage());
              }
          }
      }
	  }
	  //Zip
	  System.out.println("Enter the complete path to your project folder to zip it:");
	  String zipPath = input.nextLine();
      zipFolder(zipPath, zipPath+".zip");
      System.out.println("Folder zipped!");
   }
   //Zip the folder
   static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
	    ZipOutputStream zip = null;
	    FileOutputStream fileWriter = null;

	    fileWriter = new FileOutputStream(destZipFile);
	    zip = new ZipOutputStream(fileWriter);

	    addFolderToZip("", srcFolder, zip);
	    zip.flush();
	    zip.close();
	  }

	  static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
	      throws Exception {

	    File folder = new File(srcFile);
	    if (folder.isDirectory()) {
	      addFolderToZip(path, srcFile, zip);
	    } else {
	      byte[] buf = new byte[1024];
	      int len;
	      FileInputStream in = new FileInputStream(srcFile);
	      zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
	      while ((len = in.read(buf)) > 0) {
	        zip.write(buf, 0, len);
	      }
	    }
	  }

	  static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
	      throws Exception {
	    File folder = new File(srcFolder);

	    for (String fileName : folder.list()) {
	      if (path.equals("")) {
	        addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
	      } else {
	        addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
	      }
	    }
	  }
	    private static void downloadImage(String url, String imgSrc) throws IOException {
	        BufferedImage image = null;
	        try {
	            if (!(imgSrc.startsWith("http"))) {
	                url = url + imgSrc;
	            } else {
	                url = imgSrc;
	            }
	            imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
	            String imageFormat = null;
	            imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);
	            String imgPath = null;
	            imgPath = String.format("%s\\%s",folder, folder) + imgSrc + "";	        
	            URL imageUrl = new URL(url);
	            image = ImageIO.read(imageUrl);
	            if (image != null) {
	                File file = new File(imgPath);
	                ImageIO.write(image, imageFormat, file);
	            }
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }

	    }
}